<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

  <script>
    /*
      回顾

      1. 数组常用方法
        1-1. push()
        1-2. pop()
        1-3. unshift()
        1-4. shift()
        1-5. sort()
        1-6. reverse()
        1-7. splice()
        ======= 以上都会改变原始数组 =======
        1-8. concat()
        1-9. join()
        1-10. slice()
        1-11. indexOf()
        1-12. lastIndexOf()
        1-13. forEach()
        1-14. map()
        1-15. filter()
        1-16. every()
        1-17. some()
        1-18. find()
        1-19. findIndex()
        1-20. fill()     -      会直接改变原始数组
        1-21. includes()
        1-22. flat()
        1-23. reduce()

      2. 数组排序算法
        2-1. 冒泡排序
          for (var i = 0; i < arr.length - 1; i++) {
            var flag = false
            for (var j = 0; j < arr.length - 1 - i; j++) {
              if (arr[j] > arr[j + 1]) {
                var tmp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = tmp
                tmp = null
                flag = true
              }
            }
            if (!flag) break
          }
        2-2. 计数排序
          for (var i = 0; i < origin.length; i++) {
            newarr[origin[i]] ? newarr[origin[i]]++ : newarr[origin[i]] = 1
          }
          origin.length = 0
          for (var i = 0; i < newarr.length; i++) {
            if (!newarr[i]) continue

            for (var k = 0; k < newarr[i]; k++) {
              newarr.push(i)
            }
          }
        2-3. 选择排序
          for (var i = 0; i < arr.length - 1; i++) {
            var minIndex = i
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[j] < arr[minIndex]) minIndex = j
            }
            if (i !== minIndex) {
              var tmp = arr[i]
              arr[i] = arr[minIndex]
              arr[minIndex] = tmp
              tmp = null
            }
          }

      3. 数组去重
        3-1. 双层循环
          for (var i = 0; i < arr.length; i++) {
            for (var j = i + 1; j < arr.length; j++) {
              if (arr[i] === arr[j]) {
                arr.splice(j, 1)
                i--
              }
            }
          }
        3-2. 先排序, 在删除
          arr.sort()
          for (var i = 0; i < arr.length - 1; i++) {
            if (arr[i] === arr[i + 1]) {
              arr.splice(i + 1, 1)
            }
          }
        3-3. 新数组
          var newarr = []
          for (var i = 0; i < arr.length; i++) {
            if (!newarr.includes(arr[i])) newarr.push(i)
          }
        3-4. 利用数组索引
          var newarr = []
          for (var i = 0; i < arr.length; i++) {
            newarr[ arr[i] ] = arr[i]
          }
          for (var i = 0; i < newarr.length; i++) {
            if (!newarr[i]) {
              newarr.splice(i, 1)
              i--
            }
          }
        3-5. 利用 Set 数据结构
          var r1 = Array.from(new Set(原始数组))
          var r2 = [ ...new Set(原始数组) ]
        3-6. if 条件
          for (var i = 0; i < arr.length; i++) {
            var r = arr.indexOf( arr[i], i + 1 )

            if (r !== -1) {
              arr.splice(r, 1)
              i--
            }
          }
    */
  </script>
</body>
</html>
